home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 February: Tool Chest / Dev.CD Feb 99 TC.toast / Tool Chest / Development Kits / C++ Related / C++ Source Code Formatter / Src / ParserActions.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  8.9 KB  |  457 lines  |  [TEXT/MPS ]

  1. #ifndef __PARSERACTIONS__
  2. #define __PARSERACTIONS__    1
  3.  
  4. #ifndef __CSCANNER__
  5. #include "CScanner.h"
  6. #endif    
  7.  
  8. #ifndef __PARSER__
  9. #include "Parser.h"
  10. #endif    
  11.  
  12.  
  13. #pragma segment ParserActions
  14.  
  15.  
  16. /*
  17. ** Define the types that parse items can be.
  18. */
  19. enum {
  20.     kSLex_Flush = kSLex_Last + 1                // Unblocks Prs_Id/Prs_DeclOperator
  21.     , kSLex_Context                                // Formatting context marker
  22.  
  23.     , kSPrs_Id = kSPrs                            // Base parse type
  24.     , kSPrs_StmtList                            //
  25.     , kSPrs_DeclList                            //
  26.     , kSPrs_DeclType                            //
  27.     , kSPrs_Stmt                                //
  28.     , kSPrs_Decl                                //
  29.     , kSPrs_Do                                    //
  30.     , kSPrs_If                                    //
  31.     , kSPrs_Else                                //
  32.     , kSPrs_For                                    //
  33.     , kSPrs_Struct                                //
  34.     , kSPrs_Switch                                //
  35.     , kSPrs_While                                //
  36.     , kSPrs_Expr                                //
  37.     , kSPrs_DeclOperator                        //
  38.     , kSPrs_NewLine                                //
  39. };
  40.  
  41.  
  42. /*µ class SyntacticPrs
  43. **    This class is the abstract base class for grammar derived items.  As
  44. ** such, none of the items are separators.  The default behavior for SaveCopy
  45. ** is to return itself; this will be overridden in derived classes which
  46. ** contain state information.  The Display method will definitely be overridden.
  47. */
  48.  
  49. class SyntacticPrs : public Syntactic {
  50. public:
  51.     SyntacticPrs(int aType);
  52.  
  53.     virtual Boolean IsSeparator() const;
  54.     /*
  55.     ** Returns false.
  56.     */
  57.  
  58.     virtual const Syntactic *SaveCopy() const;
  59.     /*
  60.     ** Returns itself.
  61.     */
  62.  
  63.     virtual Boolean Display(Formatting *aFormat);
  64.     /*
  65.     ** Display is not done by the object.  Display is done by the
  66.     ** accept method.
  67.     */
  68.  
  69.  
  70.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser) = 0;
  71.     /*
  72.     ** The major parse method.  Items on the stack are handed tokens
  73.     ** and the current parser.  If the token can be used, the method
  74.     ** (possibly) modifies aParser and returns true.  If the token
  75.     ** cannot be used, the method returns false.  The parser then
  76.     ** tries the next Syntactic* on the parse stack until the stack
  77.     ** is empty, at which point the token is pushed onto the stack
  78.     ** until such time as it can be used.
  79.     */
  80. };
  81.  
  82.  
  83. //µ   SyntacticPrs::SyntacticPrs
  84. #pragma segment ParserActions
  85. inline SyntacticPrs::SyntacticPrs(int aType)
  86.     : Syntactic(aType)
  87.     {
  88.     }
  89.  
  90.  
  91.  
  92. /*µ class PrsPlaceHolder
  93. **    This class does nothing except contain a type.  Instances of this type are
  94. ** used as place holders on the parse stack.
  95. */
  96.  
  97. class PrsPlaceHolder : public SyntacticPrs {
  98. public:
  99.     PrsPlaceHolder(int aType);
  100.     PrsPlaceHolder(int aType, const char *aString);
  101.  
  102.     virtual Boolean Display(Formatting *aFormat);
  103.     virtual Boolean Accept(Syntactic *, Parser *);
  104.  
  105. private:
  106.     const char *fString;                        // Optional string to display
  107. };
  108.  
  109.  
  110. //µ   PrsPlaceHolder::PrsPlaceHolder
  111. #pragma segment ParserActions
  112. inline PrsPlaceHolder::PrsPlaceHolder(int aType)
  113.     : SyntacticPrs(aType),
  114.       fString(0)
  115.     {
  116.     }
  117.  
  118.  
  119. inline PrsPlaceHolder::PrsPlaceHolder(int aType, const char *aString)
  120.     : SyntacticPrs(aType),
  121.       fString(aString)
  122.     {
  123.     }
  124.  
  125.  
  126.  
  127. /*µ class PrsId
  128. **    This class encapsulates the concept of a qualified name, one with a
  129. ** "::" or "::*" in it.  It is through this class that kSLex_Id are
  130. ** transformed into kSLex_ParsedId.
  131. */
  132. class PrsId : public SyntacticPrs {
  133. public:
  134.     PrsId();
  135.     PrsId(Syntactic *aClassName, Syntactic *aMemberName, Syntactic *aClassOp);
  136.  
  137.     virtual Boolean Display(Formatting *aFormat);
  138.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  139.  
  140. private:
  141.     Syntactic *fClassName;
  142.     Syntactic *fMemberName;
  143.     Syntactic *fClassOp;
  144. };
  145.  
  146.  
  147. //µ   PrsId::PrsId
  148. #pragma segment ParserActions
  149. inline PrsId::PrsId()
  150.     : SyntacticPrs(kSPrs_Id),
  151.       fClassName(0),
  152.       fMemberName(0),
  153.       fClassOp(0)
  154.     {
  155.     }
  156.  
  157.  
  158. inline PrsId::PrsId(Syntactic *aClassName, Syntactic *aMemberName, Syntactic *aClassOp)
  159.     : SyntacticPrs(kSLex_ParsedId),
  160.       fClassName(aClassName),
  161.       fMemberName(aMemberName),
  162.       fClassOp(aClassOp)
  163.     {
  164.     }
  165.  
  166.  
  167.  
  168. /*µ class PrsAsId
  169. **    This class cloaks kSLex_Public keywords as respectable kSLex_ParsedId
  170. ** keywords.  Used to support C code which has variables called "public",
  171. ** "private" and "protected".
  172. */
  173. class PrsAsId : public PrsPlaceHolder {
  174. public:
  175.     PrsAsId(Syntactic *aName);
  176.  
  177.     virtual Boolean Display(Formatting *aFormat);
  178.  
  179. private:
  180.     Syntactic *fToken;                            // The token of interest.
  181. };
  182.  
  183.  
  184. //µ   PrsAsId::PrsAsId
  185. #pragma segment PrsAsId
  186. inline PrsAsId::PrsAsId(Syntactic *aName)
  187.     : PrsPlaceHolder(kSLex_ParsedId),
  188.       fToken(aName)
  189.     {
  190.     }
  191.  
  192.  
  193.  
  194. /*µ class PrsDestructor
  195. **    This class displays destructor names.  That's all.  Thank you.
  196. */
  197. class PrsDestructor : public PrsPlaceHolder {
  198. public:
  199.     PrsDestructor(Syntactic *aMemberName);
  200.  
  201.     virtual Boolean Display(Formatting *aFormat);
  202.  
  203. private:
  204.     Syntactic *fMemberName;
  205. };
  206.  
  207.  
  208. //µ   PrsDestructor::PrsDestructor
  209. #pragma segment ParserActions
  210. inline PrsDestructor::PrsDestructor(Syntactic *aMemberName)
  211.     : PrsPlaceHolder(kSLex_ParsedId),
  212.       fMemberName(aMemberName)
  213.     {
  214.     }
  215.  
  216.  
  217.  
  218. //µ class PrsStmtList
  219. class PrsStmtList : public SyntacticPrs {
  220. public:
  221.     PrsStmtList()
  222.         : SyntacticPrs(kSPrs_StmtList)
  223.         {
  224.         }
  225.  
  226.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  227. };
  228.  
  229.  
  230.  
  231. //µ class PrsDeclList
  232. class PrsDeclList : public SyntacticPrs {
  233. public:
  234.     PrsDeclList()
  235.         : SyntacticPrs(kSPrs_DeclList)
  236.         {
  237.         }
  238.  
  239.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  240. };
  241.  
  242.  
  243.  
  244. //µ class PrsStmt
  245. class PrsStmt : public SyntacticPrs {
  246. public:
  247.     PrsStmt()
  248.         : SyntacticPrs(kSPrs_Stmt)
  249.         {
  250.         }
  251.  
  252.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  253. };
  254.  
  255.  
  256.  
  257. //µ class PrsDecl
  258. class PrsDecl : public SyntacticPrs {
  259. public:
  260.     PrsDecl()
  261.         : SyntacticPrs(kSPrs_Decl)
  262.         {
  263.         }
  264.  
  265.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  266. };
  267.  
  268.  
  269.  
  270. //µ class PrsDo
  271. class PrsDo : public SyntacticPrs {
  272. public:
  273.     PrsDo()
  274.         : SyntacticPrs(kSPrs_Do)
  275.         {
  276.         }
  277.  
  278.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  279. };
  280.  
  281.  
  282.  
  283. //µ class PrsIf
  284. class PrsIf : public SyntacticPrs {
  285. public:
  286.     PrsIf()
  287.         : SyntacticPrs(kSPrs_If)
  288.         {
  289.         }
  290.  
  291.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  292. };
  293.  
  294.  
  295.  
  296. //µ class PrsElse
  297. class PrsElse : public SyntacticPrs {
  298. public:
  299.     PrsElse()
  300.         : SyntacticPrs(kSPrs_Else)
  301.         {
  302.         }
  303.  
  304.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  305. };
  306.  
  307.  
  308.  
  309. //µ class PrsFor
  310. class PrsFor : public SyntacticPrs {
  311. public:
  312.     PrsFor()
  313.         : SyntacticPrs(kSPrs_For)
  314.         {
  315.         }
  316.  
  317.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  318. };
  319.  
  320.  
  321.  
  322. //µ class PrsStruct
  323. class PrsStruct : public SyntacticPrs {
  324. public:
  325.     PrsStruct()
  326.         : SyntacticPrs(kSPrs_Struct)
  327.         {
  328.         }
  329.  
  330.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  331. };
  332.  
  333.  
  334.  
  335. //µ class PrsSwitch
  336. class PrsSwitch : public SyntacticPrs {
  337. public:
  338.     PrsSwitch()
  339.         : SyntacticPrs(kSPrs_Switch)
  340.         {
  341.         }
  342.  
  343.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  344. };
  345.  
  346.  
  347.  
  348. //µ class PrsWhile
  349. class PrsWhile : public SyntacticPrs {
  350. public:
  351.     PrsWhile()
  352.         : SyntacticPrs(kSPrs_While)
  353.         {
  354.         }
  355.  
  356.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  357. };
  358.  
  359.  
  360.  
  361. //µ class PrsExpr
  362. class PrsExpr : public SyntacticPrs {
  363. public:
  364.     PrsExpr()
  365.         : SyntacticPrs(kSPrs_Expr)
  366.         {
  367.         }
  368.  
  369.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  370. };
  371.  
  372.  
  373.  
  374. //µ class PrsDeclOperator
  375. class PrsDeclOperator : public SyntacticPrs {
  376. public:
  377.     PrsDeclOperator(int aType = kSPrs_DeclOperator)
  378.         : SyntacticPrs(aType)
  379.         {
  380.         }
  381.  
  382.     virtual Boolean Display(Formatting *aFormat);
  383.     virtual Boolean Accept(Syntactic *, Parser *);
  384.  
  385. private:
  386.     Syntactic *fToken;                            // The token of interest.
  387. };
  388.  
  389.  
  390.  
  391. /*µ class PrsNewLine
  392. **    This class implements "deferred newlines".  When source newlines are not
  393. ** being passed directly through to the output (aFormat->PassSourceNewLines()
  394. ** is false), a newline can be ambiguous.  For example, if we are to
  395. ** format "if(){}else" as
  396. **
  397. **        if () {
  398. **        } else
  399. **
  400. ** and format "if(){}foo++;" as
  401. **
  402. **        if () {
  403. **        }
  404. **        foo++;
  405. **
  406. ** and the input is "if(){} <newline>", we don't know what to do.  Because we
  407. ** emit are two or more consecutive newlines, we are not at liberty to ignore
  408. ** this newline.  We could extend the grammar and save the newline, but this
  409. ** becomes very complicated as in the particular case above the close curly
  410. ** has not been emitted yet, as it is awaiting the "else" or <other> to
  411. ** determine which glue to use for displaying it.
  412. **
  413. **    Consequently, grammar actions sensitive to these points push an instance
  414. ** of this class onto the stack when the input token is of type kSLex_NewLine.
  415. ** If the newline should have been ignored, it will be ignored.  If the newline
  416. ** should not be ignored, it will come back as being of type kSPrs_NewLine.
  417. */
  418. class PrsNewLine : public SyntacticPrs {
  419. public:
  420.     PrsNewLine()
  421.         : SyntacticPrs(kSPrs_NewLine)
  422.         {
  423.         }
  424.  
  425.     virtual Boolean IsSeparator() const;
  426.     virtual Boolean Display(Formatting *aFormat);
  427.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  428. };
  429.  
  430.  
  431. /*µ class PrsNewLineIf
  432. **    Like PrsNewLine, except it also ignores the newline if it is followed by
  433. ** "if".  Used in the context of "else <newline> if" to treat it as "else if"
  434. */
  435. class PrsNewLineIf : public PrsNewLine {
  436. public:
  437.     PrsNewLineIf()
  438.         : PrsNewLine()
  439.         {
  440.         }
  441.  
  442.     virtual Boolean Accept(Syntactic *aToken, Parser *aParser);
  443. };
  444.  
  445.  
  446.  
  447. /*
  448. ** Externed items
  449. */
  450. extern PrsDeclList gPrsDeclList;
  451. extern PrsId gPrsId;
  452. extern PrsPlaceHolder gLexFlush;
  453.  
  454. #endif    
  455.  
  456.  
  457.